<?
$GLOBALS['s']['table'] = 'mw_data';
$GLOBALS['s']['keyfor'] = 'menuitems';
$GLOBALS['s']['keyfor2'] = sqlc($_GET['menusetid']);


function listing(){
    $data = t($GLOBALS['s']['table'],$GLOBALS['s']['keyfor'],$GLOBALS['s']['keyfor2'],'');

    if($data) foreach( $data as $data_k => $data_v){
        $data[$data_k]['fid'] = name($data_v['fid']);
    }


    // list for show listing
    f('Title,text,name','','req lang');

    // conver list to arrange for form system
    if($GLOBALS['form_gen_save']) {
        $formlist = $GLOBALS['form_gen_save'];
        if($formlist) foreach( $formlist as $formlist_k => $formlist_v){
            $exp = explode(',',$formlist_v[0]);
            $namelist[] = $exp[0];
            $fieldlist[] = $exp[2];
        }
    }

    tc(	a2s($namelist),
        a2s($fieldlist),$data,
        array(
            'Photos' => url_update('','page',PAGE_LAST.'/menuitems_photo').'&fid=' ,
            'Edit' => url_update('','page',PAGE_LAST.'/edit').'&id=' ,
            'Remove' => url_update('','page',PAGE_LAST.'/listing').'&remove_id='
        )
    );

    page_title(strtoupper('menuitems'));
}

function edit(){
    model('db_model');
    $catdb = new db_model('mw_data','menuitems_cat'.sqlc($_GET['menusetid']) );
    $cat_data = $catdb->listing(' AND fid = "'.sql_clean($_GET['fid']).'"');

    if($cat_data) foreach( $cat_data as $cat_data_k => $cat_data_v){
        $cat_data_v['img'] = callfile($cat_data_v['img'],'img size200x');
        $cat_data2[] = $cat_data_v;
        if($subcheck = $catdb->listing(' and fid = "'.$cat_data_v['id'].'"')) {
            if($subcheck) foreach( $subcheck as $subcheck_k => $subcheck_v){
                $subcheck_v['name'] = '&nbsp;&nbsp; >> '.$subcheck_v['name'];
                $subcheck_v['sorting'] = '&nbsp;&nbsp; >> '.$subcheck_v['sorting'];
                $cat_data2[] = $subcheck_v;
            }

        }
    }

    if($cat_data2) foreach( $cat_data2 as $cat_data2_k => $cat_data2_v){
        $cat[$cat_data2_v['id']] = $cat_data2_v['name'];
    }

    if(!$_POST['formgen_submit']) {
        if(!$_GET['id']) {

            $_POST['data']['active'] = 1;
        }
    }

    f('Title,text,name','','lang');
    f();
    f();
    f('Content,textarea,content','','editor lang');
    f();
    f();

    f('Date,datepick,date','','');
    f();
    f();

    f('Photo,upload,img','','');

    f('Active,on_off,active','','');

    fc($GLOBALS['s']['table'],$GLOBALS['s']['keyfor'],$GLOBALS['s']['keyfor2'],strtoupper('menuitems'));
}


//<!--▼▼menuitems_cat▼▼-->
function menuitems_cat(){
    $GLOBALS['s']['table'] = 'mw_data';
    $GLOBALS['s']['keyfor'] = 'menuitems_cat'.sqlc($_GET['menusetid']) ;
    $GLOBALS['s']['keyfor2'] = '';

    model('db_model');
    $db = new db_model('mw_data','menuitems_cat'.sqlc($_GET['menusetid']) );

    $data = t($GLOBALS['s']['table'],$GLOBALS['s']['keyfor'],$GLOBALS['s']['keyfor2'],' AND fid = "0"');

    if($data) foreach( $data as $data_k => $data_v){
        $data2[] = $data_v;
        if($subcheck = $db->listing(' and fid = "'.$data_v['id'].'"')) {
            if($subcheck) foreach( $subcheck as $subcheck_k => $subcheck_v){
                $subcheck_v['name'] = '&nbsp;&nbsp; >> '.$subcheck_v['name'];
                $subcheck_v['sorting'] = '&nbsp;&nbsp; >> '.$subcheck_v['sorting'];
                $data2[] = $subcheck_v;
            }
        }
    }

    tc(	'Category,Sorting',
        'name,sorting',$data2,
        array(
            'Edit' => url_update('','page',PAGE_LAST.'/menuitems_cat_edit&fid='.$_GET['fid']).'&id='  ,
            'Remove' => url_update('','page',PAGE_LAST.'/menuitems_cat&fid='.$_GET['fid']).'&remove_id='  ,
        ),
        array(
            'Add' => p(PAGE_LAST.'/menuitems_cat_edit&fid='.$_GET['fid'])
        )
    );
    page_title(strtoupper('menuitems'));
}


function menuitems_cat_edit(){
    $GLOBALS['s']['table'] = 'mw_data';
    $GLOBALS['s']['keyfor'] = 'menuitems_cat'.sqlc($_GET['menusetid']) ;
    $GLOBALS['s']['keyfor2'] = '';


    model('db_model');
    $db = new db_model('mw_data','menuitems_cat'.sqlc($_GET['menusetid']) );
    $data = $db->listing(' AND fid = "'.sql_clean($_GET['fid']).'"');

    $data2[] = '';
    if($data) foreach( $data as $data_k => $data_v){
        $data2[] = $data_v;
        if($subcheck = $db->listing(' and fid = "'.$data_v['id'].'"')) {
            if($subcheck) foreach( $subcheck as $subcheck_k => $subcheck_v){
                $subcheck_v['name'] = '&nbsp;&nbsp; >> '.$subcheck_v['name'];
                $subcheck_v['sorting'] = '&nbsp;&nbsp; >> '.$subcheck_v['sorting'];
                $data2[] = $subcheck_v;
            }
        }
    }

    if($data2) foreach( $data2 as $data2_k => $data2_v){
        $data3[$data2_v['id']] = $data2_v['name'];
    }

    if($_GET['id']==$_POST['data']['fid'] && $_POST['data']) {
        $_POST['data']['fid'] = 0;
    }

    f('Name,text,name','','req');
    f('Sorting,text,sorting','','');

    $_SESSION['backlink'] = p( PAGE_LAST.'/menuitems_cat&fid='.$_GET['fid']);
    fc($GLOBALS['s']['table'],$GLOBALS['s']['keyfor'],$GLOBALS['s']['keyfor2'],strtoupper('menuitems'));
}

//<!--▼▼photos▼▼-->
function menuitems_photo(){
    $GLOBALS['s']['table'] = 'mw_data';
    $GLOBALS['s']['keyfor'] = 'menuitems_photo'.sqlc($_GET['menusetid']) ;
    $GLOBALS['s']['keyfor2'] = '';

    $data = t($GLOBALS['s']['table'],$GLOBALS['s']['keyfor'],$GLOBALS['s']['keyfor2'],' AND fid = "'.sql_clean($_GET['fid']).'"');


    tc(	'Name,Image,Sorting',
        'name,img,sorting',$data,
        array(
            'Edit' => url_update('','page',(PAGE_LAST.'/menuitems_photo_edit')).'&id=' ,
            'Remove' => url_update('','page',(PAGE_LAST.'/menuitems_photo')).'&remove_id=' ,
        ),
        array(
            'Add' => url_update('','page',(PAGE_LAST.'/menuitems_photo_edit'))
        )
    );
    page_title(strtoupper('menuitems'));
}


function menuitems_photo_edit(){
    $GLOBALS['s']['table'] = 'mw_data';
    $GLOBALS['s']['keyfor'] = 'menuitems_photo'.sqlc($_GET['menusetid']) ;
    $GLOBALS['s']['keyfor2'] = '';

    model('db_model');
    $catdb = new db_model('mw_data','article_cat');
    $catdata = $catdb->listing();
    if($catdata) foreach( $catdata as $catdata_k => $catdata_v){
        $cdata[$catdata_v['id']] = $catdata_v['name'];
    }

    f('name,text,name');
    f('Image,upload,img','','size800x800crop');
    f('Sorting,text,sorting','','');
    f(',hidden,fid',$_GET['fid'],'');

    $burl = url_update('','page',PAGE_LAST.'/menuitems_photo');
    $burl = url_remove($burl,'id');
    $_SESSION['backlink'] = $burl;
    fc($GLOBALS['s']['table'],$GLOBALS['s']['keyfor'],$GLOBALS['s']['keyfor2'],strtoupper('menuitems'));
}
	